<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>简单加法计算器</title>
        <style>
            .calculator {
                max-width: 300px;
                margin: 50px auto;
                padding: 20px;
                border: 1px solid #ccc;
                border-radius: 5px;
            }

            .input-group {
                margin-bottom: 10px;
            }

            input {
                width: 100%;
                padding: 5px;
                margin-bottom: 10px;
            }

            button {
                width: 100%;
                padding: 10px;
                background-color: #4caf50;
                color: white;
                border: none;
                border-radius: 3px;
                cursor: pointer;
            }

            button:hover {
                background-color: #45a049;
            }

            #result {
                margin-top: 10px;
                font-size: 18px;
                text-align: center;
            }
        </style>
    </head>

    <body>
        <div class="calculator">
            <div class="input-group">
                <input type="number" id="num1" placeholder="请输入第一个数" />
                <input type="number" id="num2" placeholder="请输入第二个数" />
            </div>
            <button onclick="calculate()">计算</button>
            <div id="result">结果：</div>
        </div>

        <script>
            let result = 10;
            function calculate(params1, params2) {
                const num1 =
                    Number(params1) ||
                    parseFloat(document.getElementById("num1").value);
                const num2 =
                    Number(params2) ||
                    parseFloat(document.getElementById("num2").value);
                if (isNaN(num1) || isNaN(num2)) {
                    document.getElementById("result").textContent =
                        "请输入有效的数字！";
                    return;
                }
                const sum = num1 + num2;
                result = sum;

                document.getElementById("result").textContent = `结果：${sum}`;
            }

            function changeTime(num) {
                calculate(num, result);
            }

            // 监听来自父页面的消息
            window.addEventListener("message", function (event) {
                console.log("子页面监听父页面：", event);
                // 处理接收到的消息
                if (event.data.type === "changeTime") {
                    changeTime(event.data.value);
                }
            });

            window.onload = function () {
                console.log("子页面加载完成，向父页面发送消息");
                window.parent.postMessage("iframeLoaded", "*");
            };
        </script>
    </body>
</html>
